//
//  ESTopicVC.m
//  Eyeshot
//
//  Created by admin on 15/7/1.
//  Copyright (c) 2015年 eyeshot. All rights reserved.
//

#define kCategoryNumber 5
#define kExceedLength 50
#import "ESTopicVC.h"

@interface ESTopicVC ()


@end

@implementation ESTopicVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createCollectionView];
    [self createCategoryBar];
}

- (void)createCategoryBar {
    ESCategoryBar *categoryBar = [[ESCategoryBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, CATEGORY_BAR_HEIGHT)];
    [self.view addSubview:categoryBar];
    categoryBar.delegate = self;
    _categoryBar = categoryBar;
}


#pragma mark - CollectionView

- (void) createCollectionView {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    CGFloat itemWidth = SCREEN_WIDTH;
    CGFloat itemHeight = SCREEN_HEIGHT - BAR_HEIGHT - CATEGORY_BAR_HEIGHT;
    flowLayout.itemSize = CGSizeMake(itemWidth, itemHeight);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = ZERO;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(ZERO, CATEGORY_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - BAR_HEIGHT - CATEGORY_BAR_HEIGHT) collectionViewLayout:flowLayout];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.pagingEnabled = YES;
    collectionView.showsHorizontalScrollIndicator = NO;
//    collectionView.bounces = NO;
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CollectionCell"];
    [self.view addSubview:collectionView];
    _collectionView = collectionView;
    _dataSourceArray = [[NSMutableArray alloc] initWithCapacity:0];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return kCategoryNumber;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell" forIndexPath:indexPath];
    cell.backgroundColor = ESRandomColor;
    return cell;
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint offset = scrollView.contentOffset;
    
    NSInteger index = (offset.x + SCREEN_WIDTH*0.5) / SCREEN_WIDTH;
    //拖动过大的时候index会变成5,而目前categoryBar里只有5个子控件subviews数组最大下标为4，会造成数组越界
    if (index < kCategoryNumber) {
        _categoryBar.currentPage = index;
    }
    
    if (offset.x < -kExceedLength) {
        if ([_delegate respondsToSelector:@selector(didExceedLeftBoundaryOfTopicVC:)]) {
            [_delegate didExceedLeftBoundaryOfTopicVC:self];
        }
    } else if (offset.x > (kCategoryNumber-1) * SCREEN_WIDTH + kExceedLength) {
        if ([_delegate respondsToSelector:@selector(didExceedRightBoundaryOfTopicVC:)]) {
            [_delegate didExceedRightBoundaryOfTopicVC:self];
        }
    }
}

- (void)categoryBar:(ESCategoryBar *)categroyBar didSelectFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    _collectionView.contentOffset = CGPointMake(SCREEN_WIDTH * toIndex, 0);
    
    NSString *index = [NSString stringWithFormat:@"%ld",toIndex];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"categroyBardidSelect" object:index userInfo:@{@"index": index}];
}


@end
